﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);


    codec = QTextCodec::codecForName("UTF-8");

    UI_Init();

    Timer_VideoPlay = new QTimer(this);
    connect(Timer_VideoPlay,SIGNAL(timeout()),this,SLOT(SL_TimerVideoPlay()));

    //4-CONNECT
    connect(Button_CameraIdSearch,SIGNAL(released()),this,SLOT(SL_BTNCameraIdSearch()));
    connect(Button_CameraIdOpen1,SIGNAL(released()),this,SLOT(SL_BTNCameraIdOpen1()));
    connect(Button_StartQueuePush,SIGNAL(clicked(bool)),this,SLOT(SL_BtnStartStopDectServer(bool)));
    connect(Button_DectForOnce,SIGNAL(released()),this,SLOT(SL_DectForOnce()));

    connect(&m_TcvDmHandle, SIGNAL(SN_DectFinishDMDect(cv::Point)), this, SLOT(SL_DectFinishWidgetRecall(cv::Point)));
}


void Widget::UI_Init()
{
    //3、控件
    //3.1、串口部分
    RecvText = new QPlainTextEdit(codec->toUnicode("串口在关闭状态"));
    ComboBox = new QComboBox();
    SendText = new QLineEdit(tr("_&X13;Y5;S15;"));

    QPushButton *ScanButton = new QPushButton(codec->toUnicode("扫描端口"));
    //    connect(ScanButton,SIGNAL(clicked(bool)),this,SLOT(scanport_slot()));

    OpenButton = new QPushButton(codec->toUnicode("打开端口"));
    //    connect(OpenButton,SIGNAL(clicked(bool)),this,SLOT(openport_slot()) );

    QPushButton *SendButton = new QPushButton(codec->toUnicode("发送"));
    //    connect(SendButton,SIGNAL(clicked(bool)),this,SLOT(MainSerialSendMsgEvent()) );

    //3.1.2 serial delay timer
    //    SerialDelayTimer = new QTimer(this);

    //3.2、坐标参数显示部分
    ArmValLab_label.push_back(new QLabel(tr("x")));
    ArmValLab_label.push_back(new QLabel(tr("y")));
    ArmValLab_label.push_back(new QLabel(tr("prime")));
    ArmValLab_label.push_back(new QLabel(tr("vice")));

    ArmVal_label.push_back(new QLabel(tr("0")));
    ArmVal_label.push_back(new QLabel(tr("0")));
    ArmVal_label.push_back(new QLabel(tr("0")));
    ArmVal_label.push_back(new QLabel(tr("0")));

    //3.3、滚动区域
    scrollarea = new QScrollArea();
    //scrollarea->setGeometry(3, 3, 500, 1000);
    ScrollWidget = new QWidget(scrollarea);
    ScrollWidget->setGeometry(0, 0, 300, 40);
    ScrollLayout = new QGridLayout(ScrollWidget);
    scrollarea->setWidget(ScrollWidget);

    //3.3.1-
    Scroll_button.push_back(new QPushButton(tr("1")));
    //Scroll_button[0]->setMinimumSize(40,600);
    Scroll_button[0]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_button[0],0,0);
    //    connect(Scroll_button[0],SIGNAL(clicked(bool)),this,SLOT(ScrollSerialSend_slot()));

    //3.3.2-4
    Scroll_lineedit.push_back(new QLineEdit(tr("_&X13;Y5;S15;")));
    Scroll_lineedit[0]->setMinimumSize(120,20);
    ScrollLayout->addWidget(Scroll_lineedit[0],0,1,1,3);

    //3.3.3-
    Scroll_delay_lineedit.push_back(new QLineEdit());
    Scroll_delay_lineedit[0]->setMinimumSize(30,20);
    Scroll_delay_lineedit[0]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_delay_lineedit[0],0,4);

    //3.3.5-
    Scroll_checkbox.push_back(new QCheckBox());
    //Scroll_checkbox[0]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_checkbox[0],0,5);
    Scroll_checkbox[0]->setChecked(true);

    //3.4、Scroll下方控件
    ContinSendButton = new QPushButton(codec->toUnicode("连续发送"));
    ContinSendButton->setEnabled(false);
    //    connect(ContinSendButton,SIGNAL(clicked(bool)),this,SLOT(ContinSendButtonFun()));

    StopContinSendButton = new QPushButton(codec->toUnicode("终止连续发送"));
    //StopContinSendButton->setEnabled(true);
    //    connect(StopContinSendButton,SIGNAL(clicked(bool)),this,SLOT(StopContinSendFun()));

    AddScrComButton = new QPushButton(codec->toUnicode("增加指令条数"));
    //    connect(AddScrComButton,SIGNAL(clicked(bool)),this,SLOT(AddScrCom()));

    ContinSend_Precise_Button = new QPushButton(codec->toUnicode("带校核连续发送"));
    ContinSend_Precise_Button->setEnabled(false);
    //    connect(ContinSend_Precise_Button,SIGNAL(clicked(bool)),this,SLOT(Precise_ContinSendFun()));

    ContinSend_CheckBox = new QCheckBox();
    ContinSend_CheckBox->setEnabled(false);

    ContinSend_Precise_CheckBox = new QCheckBox();
    ContinSend_Precise_CheckBox->setEnabled(false);

    ChatStatu_Label = new QLabel(tr("FreeStatu"));

    //3.5-搜索控件
    //open carmera
    Open_Close_Carmera = new QPushButton(tr("open carmera"));
    //    connect(Open_Close_Carmera,SIGNAL(clicked(bool)),this,SLOT(Open_Close_Carmera_Slot_Fun()));

    //search connect
    SearchCupButton = new QPushButton(tr("SearchCup"));
    SearchCupButton->setEnabled(false);
    //    connect(SearchCupButton,SIGNAL(clicked(bool)),this,SLOT(SearchCup_Fun()));
    SearchCupLabel = new QLabel(tr("0"));

    //AUTO run
    AutoRunButton = new QPushButton(tr("AutoRun"));
    AutoRunButton->setEnabled(false);
    //    connect(AutoRunButton,SIGNAL(clicked(bool)),this,SLOT(AutoRun_Slot()));
    AutoRunCheckBox = new QCheckBox();
    AutoRunCheckBox->setEnabled(false);
    AutoRunLabel = new QLabel(tr("0"));

    //search apart
    SearchCup_Dect_Button = new QPushButton(tr("Dect"));
    SearchCup_Dect_Button->setEnabled(false);
    //    connect(SearchCup_Dect_Button,SIGNAL(clicked(bool)),this,SLOT(SearchCup_Dect_Fun()));
    SearchCup_Dis_Button = new QPushButton(tr("Distance"));
    SearchCup_Dis_Button->setEnabled(false);
    //    connect(SearchCup_Dis_Button,SIGNAL(clicked(bool)),this,SLOT(SearchCup_Dis_Fun()));

    //3.6-cv展示区域
    //layout_CvControlShow
    //CV控件
    //3.5.1-camera control
    QLabel *Label_CamId1 = new QLabel("CAM Prime/left:");
    ComboBox_CameraId1 = new QComboBox();
    Button_CameraIdOpen1 = new QPushButton(tr("open CAM1"));
    QLabel *Label_CamId2 = new QLabel("CAM Vice/right:");
    ComboBox_CameraId2 = new QComboBox();
    Button_CameraIdOpen2 = new QPushButton(tr("open CAM2"));
    Button_CameraIdSearch = new QPushButton(tr("search CAM ID"));
    //3.5.2-dect server control
    Button_StartQueuePush = new QPushButton(tr("start queue push"));
    Button_StartQueuePush->setEnabled(false);
    Button_StartQueuePush->setCheckable(true);
    QLabel *Lable_showPictureTips = new QLabel(tr("input queue size:"));
    Lable_ShowQueueSize = new QLabel(tr("queue size:0"));
    Button_DectForOnce = new QPushButton(tr("dect once"));
    Button_DectForOnce->setEnabled(false);
    //3.5.3-dect show
    Lable_showDectPoint = new QLabel(tr("location:"));

    QGridLayout *layout_CvControlShow = new QGridLayout();
    layout_CvControlShow->addWidget(Label_CamId1,0,0,1,1);
    layout_CvControlShow->addWidget(ComboBox_CameraId1,0,1,1,1);
    layout_CvControlShow->addWidget(Button_CameraIdOpen1,1,1,1,1);
    layout_CvControlShow->addWidget(Label_CamId2,2,0,1,1);
    layout_CvControlShow->addWidget(ComboBox_CameraId2,2,1,1,1);
    layout_CvControlShow->addWidget(Button_CameraIdOpen2,3,1,1,1);

    layout_CvControlShow->addWidget(Button_CameraIdSearch,0,2);
    layout_CvControlShow->addWidget(Lable_showDectPoint,0,3);
    layout_CvControlShow->addWidget(Button_StartQueuePush,1,2);
//    layout_CvControlShow->addWidget(Lable_showPictureTips,0,2);
    layout_CvControlShow->addWidget(Lable_ShowQueueSize,1,0);
    layout_CvControlShow->addWidget(Button_DectForOnce,1,3);

    //3.7-picture展示区域
    //layout_CvPictureShow
    Lable_showPicture = new QLabel();
    //    Lable_showPicture->setPixmap();
    Lable_showDectPicture = new QLabel();
    QPixmap t_pixmap(m_showImageWidth,m_showImageHeight);
    t_pixmap.fill(Qt::transparent);//用透明色填充
    Lable_showPicture->setPixmap(t_pixmap);
    Lable_showDectPicture->setPixmap(t_pixmap);

    QGridLayout *layout_CvPictureShow = new QGridLayout();
    layout_CvPictureShow->addWidget(Lable_showPicture,0,0,1,1);
    layout_CvPictureShow->addWidget(Lable_showDectPicture,0,1,1,1);

    //5-layout
    QGridLayout *layout_location = new QGridLayout();
    QGridLayout *layout_buttongroup = new QGridLayout();
    QGridLayout *layout_serial = new QGridLayout();
    QGridLayout *layout_all = new QGridLayout();
    layout_main = new QVBoxLayout();

    //    layout_serial->addWidget(RecvText,0,0,1,3);
    layout_serial->addWidget(SendText,1,0,1,3);
    layout_serial->addWidget(ComboBox,2,0,1,3);
    layout_serial->addWidget(ScanButton,3,0,1,1);
    layout_serial->addWidget(OpenButton,3,1,1,1);
    layout_serial->addWidget(SendButton,3,2,1,1);

    for(unsigned int i=0;i<=ArmValLab_label.size()-1;i++){
        layout_location->addWidget(ArmValLab_label[i],i,0);
        layout_location->addWidget(ArmVal_label[i],i,1);
    }

    //text and send label/checkbox
    layout_buttongroup->addWidget(new QLabel(codec->toUnicode("连续发送状态:")),0,0);
    layout_buttongroup->addWidget(ContinSend_CheckBox,0,1);
    layout_buttongroup->addWidget(new QLabel(codec->toUnicode("带校核连续发送状态:")),1,0);
    layout_buttongroup->addWidget(ContinSend_Precise_CheckBox,1,1);
    layout_buttongroup->addWidget(new QLabel(codec->toUnicode("指令阶段状态:")),2,0);
    layout_buttongroup->addWidget(ChatStatu_Label,2,1);

    //text and send button
    layout_buttongroup->addWidget(AddScrComButton,0,2);
    layout_buttongroup->addWidget(StopContinSendButton,0,3);
    layout_buttongroup->addWidget(ContinSendButton,1,2);
    layout_buttongroup->addWidget(ContinSend_Precise_Button,1,3);

    //search connect
    layout_buttongroup->addWidget(new QLabel(tr("Cup Locate:")),3,0);
    layout_buttongroup->addWidget(SearchCupLabel,3,1);
    layout_buttongroup->addWidget(SearchCupButton,2,2);

    layout_buttongroup->addWidget(Open_Close_Carmera,2,3);

    //search apart
    layout_buttongroup->addWidget(SearchCup_Dect_Button,3,2);
    layout_buttongroup->addWidget(SearchCup_Dis_Button,3,3);

    //AUTO run
    layout_buttongroup->addWidget(new QLabel(tr("AutoRun:")),4,0);
    layout_buttongroup->addWidget(AutoRunLabel,4,1);
    layout_buttongroup->addWidget(AutoRunCheckBox,4,2);
    layout_buttongroup->addWidget(AutoRunButton,4,3);

//    layout_all->addWidget(RecvText,0,0,1,2);
//    layout_all->addLayout(layout_serial,1,0,1,1);
//    layout_all->addLayout(layout_location,1,1,1,1);
//    layout_all->addWidget(scrollarea,0,2,1,2);
//    layout_all->addLayout(layout_buttongroup,1,2,1,2);
    layout_all->addLayout(layout_CvPictureShow,0,4,1,1);
    layout_all->addLayout(layout_CvControlShow,1,4,1,1);
    layout_main->addLayout(layout_all);

    this->setLayout(layout_main);
    this->setWindowTitle(codec->toUnicode("TARM机械臂控制客户端"));
}

Widget::~Widget()
{
    m_TcvDmHandle.CloseCarmera();
    delete ui;
}


void Widget::SL_BTNCameraIdSearch()
{
    std::vector<unsigned int> t_cameraIdCounter;
    bool t_judge = m_TcvDmHandle.SearchCameraID(t_cameraIdCounter);
    if(!t_judge)
        return;
    for(int i=0;i<t_cameraIdCounter.size();i++)
    {
        ComboBox_CameraId1->addItem(QString("%1").arg(t_cameraIdCounter.at(i)));
        ComboBox_CameraId2->addItem(QString("%1").arg(t_cameraIdCounter.at(i)));
    }
}

void Widget::SL_BTNCameraIdOpen1()
{
    int t_i=0;
    //1- no camera to be found
    if(ComboBox_CameraId1->count() == 0)
        return;
    //2- if already open,close
    if(m_TcvDmHandle.IsCameraOpen(1)){
        qDebug()<<"Camera0 Open";
        //2.1- close cam
        m_TcvDmHandle.CloseCarmera();
        //2.2- change button tips
        Button_CameraIdOpen1->setText(tr("open"));
        //2.3- clear queue
        m_TcvDmHandle.ClearPictureInQueue();
        Lable_ShowQueueSize->setText("0");
        //2.4- Dect Server button disable and no checked
        Button_StartQueuePush->setEnabled(false);
        Button_StartQueuePush->setChecked(false);
        Button_DectForOnce->setEnabled(false);
    }
    //3- if no open , open it
    //4- start the timer to flash the sight
    else if(m_TcvDmHandle.OpenOneCamera(ComboBox_CameraId1->currentText().toInt(), t_i)){
        Timer_VideoPlay->start(30);
        Button_CameraIdOpen1->setText(tr("close"));
        Button_StartQueuePush->setEnabled(true);
    }
}

void Widget::SL_TimerVideoPlay()
{
    QImage t_image;
    cv::Mat t_imageMat;
    //1-
    if(m_TcvDmHandle.PopDectResFromOutputQueue(t_imageMat)==true)
    {
        Lable_showDectPicture->setPixmap(QPixmap::fromImage(MatToQImage(t_imageMat).scaled(m_showImageWidth,m_showImageHeight)));
    }
    //2.1- if already open, then
    //2.2- catch the picture
    //2.3- copy to outside
    //2.4- clone it to queue
    if(m_TcvDmHandle.IsCameraOpen(1))   //1-
        if(m_TcvDmHandle.CatchPicture(0x01))    //2-
            if(m_TcvDmHandle.CopyPicture(1,t_image)){   //3-
                Lable_showPicture->setPixmap(QPixmap::fromImage(t_image.scaled(m_showImageWidth,m_showImageHeight)));
//                Lable_showPicture->setScaledContents(true);
                if(Button_StartQueuePush->isChecked()){
                    m_TcvDmHandle.ClonePictureToInputQueue(0x01);    //4-
                    Lable_ShowQueueSize->setText(QString("%1").arg(m_TcvDmHandle.GetQueuePlSize()));
                }
                return;
            }
    //3- if no open, something error
    qDebug()<<"SL_TimerVideoPlay fail";
    Timer_VideoPlay->stop();
    m_TcvDmHandle.CloseCarmera();
}

void Widget::SL_BtnStartStopDectServer(bool i_judge)
{
    if(i_judge) //push
    {
        Button_DectForOnce->setEnabled(true);
    }
    else    //release
    {
        m_TcvDmHandle.ClearPictureInQueue();
        Lable_ShowQueueSize->setText("0");
        Button_DectForOnce->setEnabled(false);
    }
}

void Widget::SL_DectForOnce()
{
    cv::Vec3f i;
    cv::Point p;
    int j;
    qDebug()<<"SL_DectForOnce";
    m_TcvDmHandle.tcv_dectect(i, p, j);
}

void Widget::SL_DectFinishWidgetRecall(cv::Point point)
{
    Button_StartQueuePush->click();
    SL_BtnStartStopDectServer(false);
    if(point.x==0 && point.y==0)
        Lable_showDectPoint->setText(tr("location:no found"));
    else
        Lable_showDectPoint->setText(tr("location:(%1,%2)").arg(point.x).arg((point.y)));
}

